package com.crm.services;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import com.crm.model.jdao.IComplainDAO;
import com.crm.model.mdao.ComplaintMapper;
import com.crm.model.mdao.DictionariesMapper;
import com.crm.model.pojo.Complaint;
import com.crm.model.pojo.Orderinfo;
import com.crm.model.pojo.Timeline;
import com.crm.util.LoginUtil;
import com.github.pagehelper.PageHelper;

/**
 * 客户投诉业务类
 * 
 * @author 梁箐
 *
 */
@Service
public class ComplaintServices {

	@Autowired
	IComplainDAO iComplain;
	@Autowired
	ComplaintMapper complaintMapperDao;
	@Autowired
	DictionariesMapper dictionariesMapper;
	@Autowired
	TimeLineServices timeLineServices;

	public List<Complaint> findall() {
		return iComplain.findAll();
	}

	//当日投诉量
	public int selectcomplaintNum() {
		return complaintMapperDao.selectcomplaintNum();
	}
	
	/**
	 * 查询所有
	 * @return
	 */
	public List<Map<String, String>> selectAll() {
		return complaintMapperDao.selectAll();
	}
	/**
	 * 查询所有(导出)
	 * @return
	 */
	public List<Map<String, String>> selectAllInfo() {
		return complaintMapperDao.selectAllInfo(LoginUtil.getUser().getuDept(),LoginUtil.getUser().getuId());
	}

	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<Map<String, String>> selectPage(int pageNum, int pageSize,Integer statueID,String theme) {
		// 使用分页插件
		System.out.println("页码:" + pageNum + ",页数:" + pageSize);
		// 配置分页信息。这句话后面的第一个查询将调用分页。使用分页，对应的Service方法最好只写一个查询方法
		PageHelper.startPage(pageNum, pageSize);
		Integer curgency = null;
		Integer classifyid = null;
		Integer cresult = null;
		//对状态进行判断
		if (statueID!=null) {
			if (statueID==1405 || statueID==1406 || statueID==1407) {
				
				curgency=statueID;
			}
			if (statueID==1400 || statueID==1401 || statueID==1402 || statueID==1403) {
				classifyid=statueID;
			}
			if (statueID==1409 || statueID==1410 || statueID==1411) {
				cresult=statueID;
			}
		}
		System.out.println(curgency);
		System.out.println("编号："+statueID+"\t"+classifyid);
		// 获取记录数
		List<Map<String, String>> list = complaintMapperDao.selectAllNew(LoginUtil.getUser().getuDept(),LoginUtil.getUser().getuId(),curgency,classifyid,cresult,theme);
		System.out.println("list :"+list);
	    return list;
	}	
	
	
	//根据投诉编号查询
	public Complaint  findByid(Integer complaitID) {
		return iComplain.findById(complaitID).get();
	}
	
	
	/**
	 * 单条查询
	 * @return 
	 */
	public Map<String, String> selectByCid(Integer complaintId) {
		return complaintMapperDao.selectByCid(complaintId);
	}
	
	/**
	 * 物理删除（更改状态）
	 * @return 
	 */
	public int deleteByStatue(Integer complaintId) {
		return complaintMapperDao.deleteByStatue(complaintId);
	}
	
	
	/**
	 * 查询投诉分类(根据父类编号)
	 * @return
	 */
	public List<Map<String, String>> selectComplainttype(Integer pid) {
		return dictionariesMapper.selectByDicpar(pid);
	}
	
	/**
	 * 添加投诉
	 */
	public Complaint addComplaint(Complaint complaint) {
		Complaint save = iComplain.save(complaint);
		Timeline timeline=new Timeline();
		timeline.setTheme("客户投诉");
		timeline.setTimes(new Date());
		timeline.setCustId(save.getCustomer().getCustId());
		timeline.setDetails("客户："+save.getCustomer().getCustName()+" 投诉主题为:--"+save.getCTheme());
		timeLineServices.insertTimeLine(timeline);
		System.out.println(save);
		return save;
	}
	
}
